﻿using JT808.Gateway.Abstractions;
using JT808.Protocol;
using JT808.Protocol.Enums;
using JT808.Protocol.Extensions;
using JT808.Protocol.Extensions.JT1078.MessageBody;
using JT808.Protocol.MessageBody;

namespace JT808.Client.Customs
{
    public class ClientMessageHandler : JT808MessageHandler
    {
        public ClientMessageHandler(IJT808Config jT808Config) : base(jT808Config)
        {
            HandlerDict.Clear();
            HandlerDict.Add(0x8107, Msg0x8107);
            HandlerDict.Add(0x9205, Msg0x9205);
        }

        private byte[] Msg0x8107(JT808HeaderPackage package)
        {
            var replyPackage = JT808MsgId._0x0107.Create2019(package.Header.TerminalPhoneNo, new JT808_0x0107()
            {
                TerminalType = 0x01,
                MakerId = "saige",
                TerminalModel = "JH5S_D2",
                TerminalId = "0291918",
                Terminal_SIM_ICCID = "12345678901234567890",
                Terminal_Hardware_Version_Length = 0,
                Terminal_Hardware_Version_Num = "1",
                Terminal_Firmware_Version_Length = 3,
                Terminal_Firmware_Version_Num = "1.2",
                GNSSModule = 0,
                CommunicationModule = 0
            });
            return JT808Serializer.Serialize(replyPackage);
        }

        private byte[] Msg0x9205(JT808HeaderPackage package)
        {
            var replyPackage = new JT808Package
            {
                Header = new JT808Header
                {
                    MsgId = 0x1205,
                    TerminalPhoneNo = package.Header.TerminalPhoneNo,
                    ProtocolVersion = (byte)(package.Header.ProtocolVersion)
                },
                Bodies = new JT808_0x1205
                {
                    AVResouceTotal = 0,
                    AVResouces = new System.Collections.Generic.List<JT808_0x1205_AVResouce>()
                }
            };
            return JT808Serializer.Serialize(replyPackage);
        }

        /// <summary>
        /// 终端通用应答
        /// </summary>
        /// <param name="request"></param>
        /// <param name="result"></param>
        /// <returns></returns>
        public byte[] TerminalCommonReply(JT808HeaderPackage request, JT808TerminalResult result = JT808TerminalResult.Success)
        {
            if (request.Version == JT808Version.JTT2019)
            {
                byte[] data = JT808Serializer.Serialize(JT808MsgId._0x0001.Create_终端通用应答_2019(request.Header.TerminalPhoneNo, new JT808_0x0001()
                {
                    ReplyMsgId = request.Header.MsgId,
                    TerminalResult = result,
                    ReplyMsgNum = request.Header.MsgNum
                }));
                return data;
            }
            else
            {
                byte[] data = JT808Serializer.Serialize(JT808MsgId._0x0001.Create(request.Header.TerminalPhoneNo, new JT808_0x0001()
                {
                    ReplyMsgId = request.Header.MsgId,
                    TerminalResult = result,
                    ReplyMsgNum = request.Header.MsgNum
                }));
                return data;
            }
        }

        public override byte[] Processor(in JT808HeaderPackage request)
        {
            if (HandlerDict.TryGetValue(request.Header.MsgId, out var func))
            {
                return func(request);
            }
            else
            {
                return TerminalCommonReply(request);
            }
        }
    }
}
